package com.wqr.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Controller
public class BaseController {
	public String getAlertScript(String message) {
		if (message == null)
			message = "";
		return "alert('" + message.replace("'", "\"").replace("\n", "") + "');";
	}
	public void page(List list,Model model,String hidPageSize,String hidCurrentPageIndex) {
		int rowCount = list.size();//数据条数
		int currentPageIndex = 1;//当前页面
		int pageCount = 1;//页总数
		int pageSize = 10;//页尺寸
		
		if (hidPageSize != null) {
			pageSize = this.getIntegerParameter(hidPageSize);
		}
		if (pageSize <= 1)
			pageSize = 10;
		
		pageCount = rowCount / pageSize + (rowCount % pageSize == 0 ? 0 : 1);//整除加0，不整除加1
		if (pageCount <= 0)
			pageCount = 1;

		if (hidCurrentPageIndex != null) {
			currentPageIndex = this.getIntegerParameter(hidCurrentPageIndex);
		}
		if (currentPageIndex <= 0)
			currentPageIndex = 1;
		if (currentPageIndex > pageCount)
			currentPageIndex = pageCount;

		model.addAttribute("currentPageIndex", currentPageIndex);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("rowCount", rowCount);
		model.addAttribute("pageSize", pageSize);
	}
	// 获取整型表单参数，如果该表单参数不存在则返回0
		protected int getIntegerParameter(String name) {
			if (name != null)
				try {
					return Integer.parseInt(name);
				} catch (Exception ex) {
					return 0;
				}
			else
				return 0;
		}
}
